package com.j2ee.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

/**
 * @author JiangQikun
 * @date 2024/6/1 1:48
 */

@WebServlet("/useSession")
public class UseSessionServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 创建 Session
        final HttpSession session = req.getSession();
        // 获取数据
        System.out.println(session.getAttribute("msg"));

        // 期待客户端关闭后，再次打开获取 Session 也能相同
        final Cookie cookie = new Cookie("JESSIONID", session.getId());
        cookie.setMaxAge(60*60);
        resp.addCookie(cookie);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
